/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../tools/PacketCreator.h"
#include "PlayerInventory.h"
#include "Drop.h"
#include "Item.h"
#include "Map.h"
#include "MapDrops.h"

Drop::Drop(Map* map, Item* item, Position pos, int own, int time) :
map(map),
item(item),
owner(own),
dropTime(time),
mesos(false)
{
	this->pos = pos;
}

Drop::Drop(Map* map, Position pos, int own, int time) :
map(map),
owner(own),
dropTime(time),
mesos(true)
{
	this->pos = pos;
}

bool Drop::takeDrop(Player *player) {
	if (mesos) {
		if (!player->addMesos(amount, true)) {
			player->send(PacketCreator().dontTake());
			return false;
		}
		map->send(PacketCreator().lootDrop(id, player->getID()));
		player->send(PacketCreator().gainDrop(amount, 0, true));
	}
	else {
		if (player->getInventories()->giveItem(item, true)) {
			map->send(PacketCreator().lootDrop(id, player->getID()));
			player->send(PacketCreator().gainDrop(item->getID(), item->getAmount()));
		}
		else {
			player->send(PacketCreator().cantGetAnymoreItems());
			player->send(PacketCreator().dontTake());
			return false;
		}
	}
	return true;
}
